package com.woniuxy.exception;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class CustomExceptionResolver {
    //处理自定义的异常
    @ExceptionHandler(Exception.class)        //用于注释异常处理类，value属性指定需要拦截的异常类型
    @ResponseBody                            //和controller方法上的用法一样，会将方法中的返回值转json后返回给客户端
    public Map<String, Object> errorHandler(Exception e) {        //捕获异常并获取异常处理对象

        Map<String, Object> result = new HashMap<String, Object>();
        System.out.println("500+++++++++++++++++++++++++++++++++=");
        System.out.println("----------"+e.toString());
        result.put("code", 500);
        result.put("msg", "系统异常");    //获取异常信息

        return result;        //将异常信息响应给浏览器
    }

}
